package com.etc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.service.MusicService;
import com.etc.service.impl.MusicServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class ReportFormServlet
 */
@WebServlet("/ReportForm")
public class ReportFormServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MusicService musicService = new MusicServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReportFormServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("pie".equals(op)) {
			doPie(request,response);
		} else if ("zhu".equals(op)) {
			doZhu(request,response);
		}
		
		
		
		
	}

	private void doZhu(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Map<String, Object>> map = musicService.getStyleCount();
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(map);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(json);
	}

	private void doPie(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Map<String, Object>> map = musicService.getLikeCountGrounpByStyle();
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(map);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		
	}

}
